﻿using System.Threading.Tasks;
using Aierp.SystemEntities.System;
using Aierp.SystemRepository.Implements;
using Aierp.Utility;
using Aierp.Utility.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;

namespace Aierp.Controllers
{
    /// <summary>
    /// 数据字典管理
    /// </summary>
    [Route("api/[controller]")]
    public class DicsManageController : ControllerBase
    {
        DicCategoryRepository DicCategoryRepository { get; set; }
        DicsRepository DicsRepository { get; set; }
        SysVisitor SysVisitor { get; set; }
        public DicsManageController(DicCategoryRepository dicCategoryRepository, DicsRepository dicsRepository, SysVisitor sysVisitor)
        {
            DicCategoryRepository = dicCategoryRepository;
            DicsRepository = dicsRepository;
            SysVisitor = sysVisitor;
        }

        #region 单个词典
        /// <summary>
        /// 获取单个词典
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/values/5
        [HttpGet("{id}")]
        public async Task Get(int id)
        {
            var model = DicsRepository.FindById(id);
            if (model == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(model);
        }

        /// <summary>
        /// 获取词典列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetList")]
        public async Task GetList()
        {
            var pageParam = this.GetPageParam();
            await Response.WriteAsync(DicsRepository.GetList(pageParam.Order, pageParam.Limit, pageParam.Offset, pageParam.SearchModel));
        }

        /// <summary>
        /// 添加词典
        /// </summary>
        /// <returns></returns>
        [HttpPost("Add")]
        public async Task Add()
        {
            //获取词典
            var model = GetModel<Sys_Dics>();

            if (model.Title.IsEmpty() || model.Code.IsEmpty())
            {
                await this.WriteSpecReasonJson("添加失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = DicsRepository.Add(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            if (result == -2)
            {
                await this.WriteSpecReasonJson("添加失败，错误信息如下：系统中已经存在这个词典！");
            }
        }
        /// <summary>
        /// 更新词典
        /// </summary>
        /// <returns></returns>
        [HttpPost("Update")]
        public async Task Update()
        {
            //获取词典
            var model = GetModel<Sys_Dics>();

            if (model.Title.IsEmpty() || model.Code.IsEmpty())
            {
                await this.WriteSpecReasonJson("更新失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = DicsRepository.Update(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：系统中找不到这个词典！");
                    return;
                case -3:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：本条记录不允许修改！");
                    break;
                case -9:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：标题或者编码与系统现有记录重复！");
                    return;
            }
        }

        /// <summary>
        /// 停用词典--非物理删除
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("{id}")]
        public async Task Delete(int id)
        {
            if (id == 0)
            {
                await this.WriteSpecReasonJson("标题或编码不能为空！");
                return;
            }

            var result = DicsRepository.Remove(id);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("删除失败，错误信息如下：系统中找不到这个词典！");
                    return;
                case -3:
                    await this.WriteSpecReasonJson("删除失败，错误信息如下：本条记录不允许删除！");
                    break;
            }
        }
        #endregion

        #region 词典分类
        /// <summary>
        /// 获取单个词典分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        // GET api/values/5
        [HttpGet("GetCategory/{id}")]
        public async Task GetCategory(int id)
        {
            var model = DicCategoryRepository.FindById(id);
            if (model == null)
            {
                await WriteFailJson();
                return;
            }
            await WriteResultJson(model);
        }

        /// <summary>
        /// 获取词典分类列表
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetCategoryList")]
        public async Task GetCategoryList()
        {
            var pageParam = this.GetPageParam();
            await Response.WriteAsync(DicCategoryRepository.GetList(pageParam.Order, pageParam.Limit, pageParam.Offset, pageParam.SearchModel));
        }

        /// <summary>
        /// 添加词典分类
        /// </summary>
        /// <returns></returns>
        [HttpPost("AddCategory")]
        public async Task AddCategory()
        {
            //获取词典
            var model = GetModel<Sys_DicCategory>();

            if (model.Title.IsEmpty() || model.Code.IsEmpty())
            {
                await this.WriteSpecReasonJson("添加失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = DicCategoryRepository.Add(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            if (result == -2)
            {
                await this.WriteSpecReasonJson("添加失败，错误信息如下：系统中已经存在这个词典！");
            }
        }
        /// <summary>
        /// 更新词典分类
        /// </summary>
        /// <returns></returns>
        [HttpPost("UpdateCategory")]
        public async Task UpdateCategory()
        {
            //获取词典
            var model = GetModel<Sys_DicCategory>();

            if (model.Title.IsEmpty() || model.Code.IsEmpty())
            {
                await this.WriteSpecReasonJson("更新失败，错误信息如下：标题或编码不能为空！");
                return;
            }

            var result = DicCategoryRepository.Update(model);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：系统中找不到这个词典分类！");
                    return;
                case -3:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：本条词典分类记录不允许修改！");
                    break;
                case -9:
                    await this.WriteSpecReasonJson("更新失败，错误信息如下：词典分类标题或者编码与系统现有记录重复！");
                    return;
            }
        }

        /// <summary>
        /// 停用词典分类--非物理删除
        /// </summary>
        /// <param name="id"></param>
        // DELETE api/values/5
        [HttpDelete("DeleteCategory/{id}")]
        public async Task DeleteCategory(int id)
        {
            if (id == 0)
            {
                await this.WriteSpecReasonJson("标题或编码不能为空！");
                return;
            }

            var result = DicCategoryRepository.Remove(id);
            if (result > 0)
            {
                await this.WriteSucessJson();
                return;
            }

            //处理错误信息
            switch (result)
            {
                case -2:
                    await this.WriteSpecReasonJson("删除失败，错误信息如下：系统中找不到这个词典！");
                    break;
                case -3:
                    await this.WriteSpecReasonJson("删除失败，错误信息如下：该分类下面有明细字典，请先删除！");
                    break;
            }
        }
        #endregion
    }
}
